package flc.ast.widget.weather;

import android.annotation.SuppressLint;
import android.appwidget.AppWidgetManager;
import android.content.Context;
import android.widget.RemoteViews;

import androidx.annotation.Nullable;

import com.blankj.utilcode.util.TimeUtils;
import com.stark.weather.lib.WeatherManager;

import java.text.SimpleDateFormat;

import flc.ast.Constant;
import flc.ast.R;
import stark.common.apis.base.Weather;
import stark.common.base.IReqRetCallback;
import stark.common.basic.utils.SPUtil;

public class Weather3Widget extends BaseWeatherWidget {

    public Weather3Widget(int appWidgetId) {
        super(appWidgetId);
    }

    @Override
    protected void onUpdate(Context context, boolean success) {
        @SuppressLint("RemoteViewLayout") RemoteViews remoteViews =
                new RemoteViews(context.getPackageName(), R.layout.widget_weather3);
        if (!success) return;
        //获取天气信息
        String city = SPUtil.getString(context, Constant.WEATHER_LOCATION, "长沙");
        remoteViews.setTextViewText(R.id.tvWeather3Location, city);
        WeatherManager.getWeather(null, city, new IReqRetCallback<Weather>() {
            @Override
            public void onResult(boolean success, String msg, @Nullable Weather result) {
                if (result == null) return;
                remoteViews.setImageViewResource(R.id.ivWeather3Log, getWeatherLog(result.realtime.wid));
                remoteViews.setTextViewText(R.id.tvWeather3Temp, result.realtime.temperature + "°c");
                remoteViews.setTextViewText(R.id.tvWeather3Info, result.realtime.info);
                remoteViews.setTextViewText(R.id.tvWeather3Date, getDate(result.future.get(0).date));
                remoteViews.setImageViewResource(R.id.ivWeather3Log1, getWeatherLog(result.future.get(0).wid.day));
                remoteViews.setTextViewText(R.id.tvWeather3Info1, getDate(result.future.get(0).date));
                remoteViews.setImageViewResource(R.id.ivWeather3Log2, getWeatherLog(result.future.get(1).wid.day));
                remoteViews.setTextViewText(R.id.tvWeather3Info2, getDate(result.future.get(1).date));
                remoteViews.setImageViewResource(R.id.ivWeather3Log3, getWeatherLog(result.future.get(2).wid.day));
                remoteViews.setTextViewText(R.id.tvWeather3Info3, getDate(result.future.get(2).date));
                remoteViews.setImageViewResource(R.id.ivWeather3Log4, getWeatherLog(result.future.get(3).wid.day));
                remoteViews.setTextViewText(R.id.tvWeather3Info4, getDate(result.future.get(3).date));
                //更新数据
                AppWidgetManager.getInstance(context).updateAppWidget(appWidgetId, remoteViews);
            }
        });
    }

    private String getDate(String date) {
        long millis = TimeUtils.string2Millis(date, new SimpleDateFormat("yyyy-MM-dd"));
        return TimeUtils.millis2String(millis, new SimpleDateFormat("MM/dd"));
    }

    public static int getWeatherLog(String id) {
        int img = R.drawable.q;
        if (id.equals("00")) {
            img = R.drawable.q;//晴天
        } else if (id.equals("01")) {
            img = R.drawable.qzdf;//多云
        } else if (id.equals("02")) {
            img = R.drawable.yin;//阴
        } else if (id.equals("03")) {
            img = R.drawable.xiayu;//阵雨
        } else if (id.equals("04")) {
            img = R.drawable.ldy;//雷阵雨
        } else if (id.equals("05")) {
            img = R.drawable.ldy;//雷阵雨伴有冰雹
        } else if (id.equals("06")) {
            img = R.drawable.xiayu;//雨夹雪
        } else if (id.equals("07")) {
            img = R.drawable.xiaoyu;//小雨
        } else if (id.equals("08")) {
            img = R.drawable.xiayu;//中雨
        } else if (id.equals("09")) {
            img = R.drawable.xiayu;//大雨
        } else if (id.equals("10")) {
            img = R.drawable.xiayu;//暴雨
        } else if (id.equals("11")) {
            img = R.drawable.xiayu;//大暴雨
        } else if (id.equals("12")) {
            img = R.drawable.xiayu;//特大暴雨
        } else if (id.equals("13")) {
            img = R.drawable.qzx;//阵雪
        } else if (id.equals("14")) {
            img = R.drawable.qzx;//小雪
        } else if (id.equals("15")) {
            img = R.drawable.qzx;//中雪
        } else if (id.equals("16")) {
            img = R.drawable.qzx;//大雪
        } else if (id.equals("17")) {
            img = R.drawable.qzx;//暴雪
        } else if (id.equals("18")) {
            img = R.drawable.yin;//雾
        } else if (id.equals("19")) {
            img = R.drawable.xiayu;//冻雨
        } else if (id.equals("20")) {
            img = R.drawable.yin;//沙尘暴
        } else if (id.equals("21")) {
            img = R.drawable.xiaoyu;//小雨到中雨
        } else if (id.equals("22")) {
            img = R.drawable.xiayu;//中雨到大雨
        } else if (id.equals("23")) {
            img = R.drawable.xiayu;//大到暴雨
        } else if (id.equals("24")) {
            img = R.drawable.xiayu;//暴雨到大暴雨
        } else if (id.equals("25")) {
            img = R.drawable.xiayu;//大暴雨到特大暴雨
        } else if (id.equals("26")) {
            img = R.drawable.qzx;//小到中雪
        } else if (id.equals("27")) {
            img = R.drawable.qzx;//中到大雪
        } else if (id.equals("28")) {
            img = R.drawable.qzx;//大到暴雪
        } else if (id.equals("29")) {
            img = R.drawable.yin;//浮尘
        } else if (id.equals("30")) {
            img = R.drawable.yin;//扬沙
        } else if (id.equals("31")) {
            img = R.drawable.yin;//强沙尘暴
        } else if (id.equals("53")) {
            img = R.drawable.yin;//霾
        }
        return img;
    }
}
